package homework20240826.dao.Impl;

import homework20240826.utils.DBUtil;
import homework20240826.dao.IJdbcDao;
import homework20240826.pojo.Jdbc;

import java.sql.*;

public class JdbcDaoImpl implements IJdbcDao {
    Connection connection;
    int result = 0;


    private void state() {
        String url = "jdbc:mysql://127.0.0.1:3306/a65?serverTimezone=UTC";
        String username = "root";
        String password = "415253";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int insertJdbc(Jdbc jdbc) {
        state();
        String sql = "insert into jdbc (name,pwd,email,birthday,money) values(?,?,?,?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, jdbc.getName());
            preparedStatement.setString(2, jdbc.getPwd());
            preparedStatement.setString(3, jdbc.getEmail());
            preparedStatement.setDate(4, jdbc.getBirthday());
            preparedStatement.setInt(5, jdbc.getMoney());
            result = preparedStatement.executeUpdate();
            DBUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int deleteJdbc(Jdbc jdbc) {
        state();
        String sql = "delete from jdbc where name=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, jdbc.getName());
            result = preparedStatement.executeUpdate();
            DBUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateJdbc(Jdbc jdbc) {
        state();
        String sql = "update jdbc set birthday=CURDATE() where name=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, jdbc.getName());
            result = preparedStatement.executeUpdate();
            DBUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public ResultSet selectJdbc(Jdbc jdbc) {
        state();
        ResultSet rs = null;
        String sql = "select * from jdbc where birthday > '1986-12-30'";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int Transfer(String name1, String name2, int money) {
        Connection connection = DBUtil.getConnection();
        String sql1 = "update jdbc set money=money-? where name=?";
        String sql2 = "update jdbc set money=money+? where name=?";
        try {
            connection.setAutoCommit(false);
            PreparedStatement preparedStatement = connection.prepareStatement(sql1);
            preparedStatement.setInt(1, money);
            preparedStatement.setString(2, name1);
            preparedStatement.executeUpdate();
            PreparedStatement preparedStatement1 = connection.prepareStatement(sql2);
            preparedStatement1.setInt(1, money);
            preparedStatement1.setString(2, name2);
            preparedStatement1.executeUpdate();
            connection.commit();
            System.out.println("转账成功");
            preparedStatement.close();
            DBUtil.close(preparedStatement1, connection);
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                System.out.println(111);
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return 0;
    }

}
